package com.saxon.system.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.asymmetric.AsymmetricCrypto;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.http.server.HttpServerResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.saxon.system.constant.HttpStatus;
import com.saxon.system.constant.RsaSecret;
import com.saxon.system.service.SysAuthUserService;
import com.saxon.system.service.SysUserService;
import com.saxon.system.utils.AjaxResult;
import com.saxon.system.utils.JwtTokenUtil;
import com.saxon.system.utils.RedisUtil;
import com.wf.captcha.ArithmeticCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 登录类
 *
 * @author saxon
 */
@Slf4j
@RestController
@RequestMapping ("/auth")
public class SysAuthUserController {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    JwtTokenUtil jwtTokenUtil;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysAuthUserService sysAuthUserService;

    /**
     * 登录
     *
     * @param data 前端传来的数据
     * @return
     */
    @PostMapping ("/login")
    public AjaxResult login (@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject (data);
        //返回信息
        String msg = "";
        //获得密码
        String password = (String) jsonObject.get ("password");
        //密码解析
        AsymmetricCrypto asymmetricCrypto = new AsymmetricCrypto ("RSA", RsaSecret.privateKey, RsaSecret.publicKey);
        String s = asymmetricCrypto.decryptStr (password, KeyType.PrivateKey);
        //验证码验证
        String key = (String) jsonObject.get ("uuid");
        String RedisVerCode = (String) redisUtil.get (key);
        String vercode = (String) jsonObject.get ("code");
        AjaxResult ajaxResult = AjaxResult.success ();
        if (RedisVerCode == null) {
            msg = "验证码已过期，请刷新";
        } else if (! RedisVerCode.equals (vercode)) {
            msg = "验证码错误";
        } else {
            ajaxResult.put ("code", HttpStatus.SUCCESS);
        }
        //数据传输
        ajaxResult.put ("msg", msg);
        ajaxResult.put ("user", null);
        Map<String, String> cliams = new HashMap<> ();
        cliams.put ("username", "user");
        //生成token
        ajaxResult.put ("token", jwtTokenUtil.generateToken (key, cliams));
        return ajaxResult;
    }

    @GetMapping ("/info")
    public String info () {
        return "info ok";
    }

    /**
     * 获得验证码
     *
     * @return
     */
    @GetMapping ("/code")
    public AjaxResult code (HttpServerResponse httpServerResponse) {
        AjaxResult ajaxResult = AjaxResult.success ();
        //生成验证码
        ArithmeticCaptcha specCaptcha = new ArithmeticCaptcha (130, 48, 2);
        //获得计算结果
        String verCode = specCaptcha.text ();
        String key = IdUtil.simpleUUID ();
        // 存入redis并设置过期时间为5min,key就是uuid
        try {
            redisUtil.set (key, verCode, 300);
        } catch (Exception e) {
            httpServerResponse.sendError (HttpStatus.ERROR, "redis错误");
        }
        ajaxResult.put ("img", specCaptcha.toBase64 ());
        ajaxResult.put ("uuid", key);
        return ajaxResult;
    }

    @DeleteMapping ("/logout")
    public String logout () {
        return "logout";
    }


    @GetMapping ("/test")
    public String getUser () {
        return "";
    }
}
